﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GraphQLSample.Domain.Core;

namespace GraphQLSample.Domain.Services
{
    public class UserService
        : IUserService
    {
        private List<User> _users;
        private readonly IUserEventService _userEventService;
        public UserService(IUserEventService userEventService)
        {
            _userEventService = userEventService;
            _users = new List<User>();
            for (int i = 0; i < 5; i++)
            {
                _users.Add(new User()
                {
                    Name = Guid.NewGuid().ToString("N"),
                    RoleId = (i + 1).ToString(),
                });
            }
        }

        public bool CreateUser(string name)
        {
            _users.Add(new User() { Name = name });
            _userEventService.AddEvent(new UserEvent()
            {
                 Name=name
            });
            return true;
        }

        public async Task<IEnumerable<User>> GetAsync()
        {
            return await Task.FromResult(_users);
        }

        public Task<User> GetByIdAsync(string id)
        {
            return Task.FromResult(_users.SingleOrDefault(u => u.Id.Equals(id)));
        }
    }
}
